// pages/ordersDetail/myOrders.js

var apiHost = wx.getStorageSync('apiHost');
var yunId = "";
var orderNo = "";
var openId = "";
var detailList = "";
var acceptTime = "";
var orderCreTime = "";
var blackStar = 0;
var leftTime = "";
var paylefttime  = "";
var flag = "";
var types = "";
var deliveryLimitTime  = "";
var payLimitTime = "";
var limitTime = "";
var sendleftTime  = "";
var list = new Array();
var paytype = "";
var orderstate = "";
var util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: true,    //loading
    showModal: false,
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    var that = this;
    //显示加载动画
    that.setData({
      hiddenLoading: false
    });
    yunId = options.yunId,
      orderNo = options.orderNo,
      openId = options.openId
    that.setData({
      yunId: yunId,
      orderNo: orderNo,
      openId: openId
    })
    that.orderDetail();
    /**
         * 获取系统信息
         */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          hiddenLoading: true
        });
      }
    });
  },
  call: function (event) {
    var phone = event.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })
  },
  delOrder: function (openId) {
    var that = this;
    that.setData({
      showModal: true
    })
  },
  cancel: function () {
    this.setData({
      showModal: false
    })
  },
  confirm: function (event) {
    var that = this;
    wx.request({
      url: apiHost + 'xcx/order/del',
      data: {
        yunId: yunId,
        orderNo: orderNo,
        openId: openId
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        that.setData({
          showModal: false
        })
        if (res.data.code == "10000") {
          wx.showToast({
            title: '删除成功!',
            icon: 'success',
            duration: 2000
          })
          // 跳转订单列表
          wx.navigateTo({
            url: '/pages/order/order',
            data: {
              yunId: yunId,
              openId: openId
            }
          })
        }
      }
    })
  },
  refuse: function (openId) {
    var that = this;
    var state = 0;
    wx.request({
      url: apiHost + 'xcx/order/confirmAcceptOrder',
      data: {
        yunId: yunId,
        state: state,
        orderNo: orderNo,
        openId: openId,
        'type':type
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        console.log("34355345");
        console.log(res);
        if (res.data.code == "10000") {
          that.orderDetail();
        }
      },
    })
  },
  accept: function (e) {
    var state = 1;
    var that = this;
    wx.request({
      url: apiHost + 'xcx/order/confirmAcceptOrder',
      data: {
        yunId: yunId,
        state: state,
        orderNo: orderNo,
        openId: openId
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        if (res.data.code == "10000") {
          that.orderDetail();
        }
      },
    })
  },
  orderDetail: function (openId) {
    var that = this;
    wx.request({
      url: apiHost + 'xcx/order/detail',
      data: {
        yunId: yunId,
        orderNo: orderNo,
        openId: openId
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        console.log("55555")
        console.log(res)
        var leftTime = null;
        var star = ""
        if (res.data.code == "10000") {
          blackStar = 5 - res.data.data.star;
          limitTime = res.data.data.limitTime;
          orderCreTime = res.data.data.createTime;
          payLimitTime = res.data.data.payLimitTime;
          // acceptTime = res.data.data.acceptTime;
          list = res.data.data;
          if (res.data.data.deliveryType == "2") {
            acceptTime = res.data.data.acceptTime;
            deliveryLimitTime = res.data.data.deliveryLimitTime
          }
          orderstate = res.data.data.state
        }
        paytype = res.data.data.payType
        if (paytype == "0") {
          paytype = "货到付款"
        }
        if (paytype == "1") {
          paytype = "微信支付"
        }
        
        that.setData({
          detailList: res.data.data,
          blackStar: blackStar,
          paytype: paytype
        })
        if (list.state == '1') {
          //获取底部按钮区域
          /** 
             * 获取系统信息 
             */
          wx.getSystemInfo({
            success: function (res) {
              that.setData({
                winWidth: res.windowWidth,
                winHeight: res.windowHeight - 45,
                hiddenLoading: true
              });
            }
          });
        }
      },
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  // 页面渲染完成后 调用  
  onReady: function () {
    var intervals = setInterval(function () {
      var that = this;
      leftTime = "";
      var nowTime = util.formatTime(new Date()).toString();
      var Nhour = nowTime.substring(11, 13) * 1;
      var Nmin = nowTime.substring(14, 16) * 1;
      var Nsec = nowTime.substring(17, 19) * 1;
      types = "2";
      if (list.state == "1") {
        var payTime = list.payTime;
        orderNo = list.orderNo;
        var createtime = list.createTime;
        if (list.payType == "0") {
          var Emin = createtime.substring(14, 16) * 1 + limitTime * 1;
          var Ehour = createtime.substring(11, 13) * 1;
          var Esec = createtime.substring(17, 19) * 1;
        } else {
          var Emin = payTime.substring(14, 16) * 1 + limitTime * 1;
          var Ehour = payTime.substring(11, 13) * 1;
          var Esec = payTime.substring(17, 19) * 1;
        }
        if (Emin > 60) {
          Emin = Emin - 60;
          Ehour = Ehour + 1;
        }
        var Lmin = 0;
        var Lsec = 0;
        var Lhour = 0;
        if (Esec >= Nsec) {
          Lsec = Esec - Nsec;
        } else {
          Esec = Esec + 60;
          Lsec = Esec - Nsec;
          Emin = Emin - 1;
        }
        if (Emin >= Nmin) {
          Lmin = Emin - Nmin;
        } else {
          Emin = Emin + 60;
          Lmin = Emin - Nmin;
          Ehour = Ehour - 1;
        }
        Lhour = Ehour - Nhour;
        //当前时间是否大于过期时间判断
        var flags = false;
        if (Nhour > Ehour) {
          flags = true;
          // console.log("11111$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        } else {
          flags = false;
        }
        if (Nhour == Ehour) {
          // console.log("######");
          if (Nmin > Emin) {
            flags = true;
            // console.log("2222$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
          } else {
            flags = false;
          }
          if (Nmin == Emin) {
            if (Nsec > Esec) {
              flags = true;
              // console.log("3333$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
            } else {
              flags = false;
            }
          }
        }
        // console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        // console.log(flags);
        if ((Lmin == 0 && Lsec == 0) || flags) {
          console.log("5555");
          wx.request({
            url: apiHost + 'xcx/order/confirmAcceptOrder',
            data: {
              yunId: yunId,
              openId: openId,
              orderNo: orderNo,
              state: '0',
              'type': types
            },
            header: {
              'conttent-type': 'application-json'
            },
            success: function (res) {
              that.orderDetail();
            }
          })
        }
        if (Lmin < limitTime * 1) {
          console.log("%%%%%");
          leftTime = Lmin + "分" + Lsec + "秒";
          console.log(leftTime);
        }
      }

      that.setData({
        leftTime: leftTime
      })
      // console.log("**************")
      // console.log(list)
      //待付款倒计时
      if (list.state == "0") {
        // console.log("8888888888")
        var that = this;
        var nowTime = util.formatTime(new Date()).toString();

        var Emin = orderCreTime.substring(14, 16) * 1;
        var Ehour = orderCreTime.substring(11, 13) * 1 + payLimitTime * 1;
        var Esec = orderCreTime.substring(17, 19) * 1;

        var NHour = nowTime.substring(11, 13) * 1;
        var Nmin = nowTime.substring(14, 16) * 1;
        var Nsec = nowTime.substring(17, 19) * 1;
        var LSec = 0;
        var LMin = 0;
        var LHour = 0;

        if (Esec >= Nsec) {
          LSec = Esec - Nsec;
        } else {
          Esec = Esec + 60;
          LSec = Esec - Nsec;
          Emin = Emin - 1;
        }
        if (Emin >= Nmin) {
          LMin = Emin - Nmin;
        } else {
          Emin = Emin + 60
          LMin = Emin - Nmin;
          Ehour = Ehour - 1;
        }
        if (Ehour >= NHour) {
          LHour = Ehour - NHour;
          if (LHour <= deliveryLimitTime * 1) {
            paylefttime = LHour + "时" + LMin + "分" + LSec + "秒";
            that.setData({
              paylefttime: paylefttime
            })
          }
        }
      }
    }.bind(this), 1000);

    if (typeof (acceptTime) != "undefined") {
      var interval = setInterval(function () {
        var that = this;
        var nowTime = util.formatTime(new Date()).toString();
        var Nhour = nowTime.substring(11, 13) * 1;
        var Nmin = nowTime.substring(14, 16) * 1;
        var Nsec = nowTime.substring(17, 19) * 1;

        var Emin = acceptTime.substring(14, 16) * 1;
        var Ehour = acceptTime.substring(11, 13) * 1 + deliveryLimitTime * 1;
        var Esec = acceptTime.substring(17, 19) * 1;


        var Lmin = 0;
        var Lsec = 0;
        var Lhour = 0;
        if (Esec >= Nsec) {
          Lsec = Esec - Nsec;
        } else {
          Esec = Esec + 60;
          Lsec = Esec - Nsec;
          Emin = Emin - 1;
        }
        if (Emin >= Nmin) {
          Lmin = Emin - Nmin;
        } else {
          Emin = Emin + 60
          Lmin = Emin - Nmin;
          Ehour = Ehour - 1;
        }
        if (Ehour >= Nhour) {
          Lhour = Ehour - Nhour;
          if (Lhour <= deliveryLimitTime * 1) {
            sendleftTime = Lhour + "时" + Lmin + "分" + Lsec + "秒";
            that.setData({
              sendleftTime: sendleftTime
            })
          }
        }
      }.bind(this), 1000);
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (flag == "list") {
      wx.switchTab({
        url: '/pages/order/order'
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
  },
})
